//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.Promotion;
import com.aidong.model.wills.PromotionCardType;
import com.aidong.model.wills.PssProductCode;
import java.util.Iterator;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

@Repository
public class PromotionDao extends BaseDao<Promotion, Integer> {
	public PromotionDao() {
	}

	public List<Promotion> getShopPromotionList(String shopCode) {
		String sql = "select p.promotionId, chName from Promotion p inner join PromotionShop s on p.promotionId=s.promotionId\nwhere shopCode=:p1 and status=1 and ActiveTime<getdate() and InactiveTime>getdate()";
		return this.queryListBySql(Promotion.class, sql, new Object[]{shopCode});
	}

	public List<DicShopCode> getShopList(int promotionId, boolean ball) {
		String sql;
		if(ball) {
			sql = "select s.code, s.chName, (case when a.shopCode is null then 0 else 1 end) status from DicShopCode s\nleft join PromotionShop a on s.code=a.shopCode and promotionId = :p1\nwhere status>0 order by s.code";
		} else {
			sql = "select s.code, s.chName, status from DicShopCode s\ninner join PromotionShop a on s.code=a.shopCode\nwhere promotionId = :p1 and status>0 order by s.code";
		}

		return this.queryListBySql(DicShopCode.class, sql, new Object[]{Integer.valueOf(promotionId)});
	}

	public List<PssProductCode> getProductList(int promotionId, boolean ball) {
		String sql;
		if(ball) {
			sql = "select s.productCode, s.productName, price, (case when a.productCode is null then 0 else 1 end) status from PssProductCode s\nleft join PromotionProduct a on s.productCode=a.productCode and promotionId = :p1\nwhere status>0 and saleFlg=\'2\' order by s.productCode";
		} else {
			sql = "select s.productCode, s.productName, price, status from PssProductCode s\ninner join PromotionProduct a on s.productCode=a.productCode\nwhere promotionId = :p1 and status>0 and saleFlg=\'2\' order by s.productCode";
		}

		return this.queryListBySql(PssProductCode.class, sql, new Object[]{Integer.valueOf(promotionId)});
	}

	public void addPromotion(Promotion promotion, List<String> shopList, List<String> productList, List<PromotionCardType> cardList) {
		this.add(promotion);
		this.saveDetail(promotion.getPromotionId().intValue(), shopList, productList, cardList);
	}

	public void updatePromotion(Promotion promotion, List<String> shopList, List<String> productList, List<PromotionCardType> cardList) {
		this.update(promotion);
		this.saveDetail(promotion.getPromotionId().intValue(), shopList, productList, cardList);
	}

	private void saveDetail(int promotionId, List<String> shopList, List<String> productList, List<PromotionCardType> cardList) {
		Session session = this.getSession();
		SQLQuery query = session.createSQLQuery("delete from PromotionShop where promotionId = :p1");
		query.setInteger("p1", promotionId);
		query.executeUpdate();
		query = session.createSQLQuery("insert into PromotionShop (promotionId, ShopCode) values (:p1, :p2)");
		query.setInteger("p1", promotionId);
		Iterator i$ = shopList.iterator();

		String cardType;
		while(i$.hasNext()) {
			cardType = (String)i$.next();
			query.setString("p2", cardType);
			query.executeUpdate();
		}

		query = session.createSQLQuery("delete from PromotionProduct where promotionId = :p1");
		query.setInteger("p1", promotionId);
		query.executeUpdate();
		query = session.createSQLQuery("insert into PromotionProduct (promotionId, productCode) values (:p1, :p2)");
		query.setInteger("p1", promotionId);
		i$ = productList.iterator();

		while(i$.hasNext()) {
			cardType = (String)i$.next();
			query.setString("p2", cardType);
			query.executeUpdate();
		}

		i$ = cardList.iterator();

		while(true) {
			while(i$.hasNext()) {
				PromotionCardType cardType1 = (PromotionCardType)i$.next();
				if(cardType1.getPromotionId() != null && cardType1.getPromotionId().intValue() == -1) {
					PromotionCardType tmp = (PromotionCardType)this.getSession().get(PromotionCardType.class, cardType1.getAutoId());
					this.getSession().delete(tmp);
				} else {
					cardType1.setPromotionId(Integer.valueOf(promotionId));
					this.getSession().saveOrUpdate(cardType1);
				}
			}

			return;
		}
	}

	public boolean updateStatus(int promotionId, int status) {
		String hql = "update Promotion set status=:p1 where promotionId=:p2";
		return this.updateByHql(hql, new Object[]{Integer.valueOf(status), Integer.valueOf(promotionId)}) > 0;
	}
}
